//
//  TJTopUpRecordController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/17.
//  Copyright © 2020 xtjun. All rights reserved.
//充币记录VC

#import "TJTopUpRecordController.h"
#import "TJTopUpRecordCell.h"
#import "TJTransferDetailController.h"

@interface TJTopUpRecordController ()

@property (strong, nonatomic) NSMutableArray *datas;
@property (assign, nonatomic) int pageIndex;


@end

static NSString * const topUpRecordCell = @"topUpRecordCell";


@implementation TJTopUpRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.backgroundColor = UIColor.whiteColor;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 90;
    [self.tableView registerClass:TJTopUpRecordCell.self forCellReuseIdentifier:topUpRecordCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.offset(0);
        make.bottom.offset(0);
    }];
    
 
    [self setUpData];
}
- (void)setUpData{
    [self loadNewData];

    self.tableView.mj_header = self.refreshHeader;
    self.tableView.mj_footer = self.refreshFooter;
 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJTopUpRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:topUpRecordCell forIndexPath:indexPath];
    cell.model = self.datas[indexPath.row];
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    NSDictionary *dict = self.datas[indexPath.row];
//    if (!dict) return;
//    
//    TJTransferDetailController *transferDetailController = [[TJTransferDetailController alloc] init];
//    transferDetailController.dict = dict;
////    transferDetailController.coinNo = _coinNo;
//    [self.navigationController pushViewController:transferDetailController animated:YES];
//    
//    
//}

- (void)loadNewData{
    _pageIndex = 1;
 
    NSDictionary *parameters = @{

        @"size": @(10),
        @"userId": TJUserInfo.readInfo.userId,
        @"page": @(_pageIndex)
     
    };
    
    NSString *url = @"";
    if (self.type == 0) {//提币
        url = @"member/balance/withdraw_recording";
    }else{//充币
         url = @"member/balance/recharge_recording";
    }
    
    weakify(self);
    [TJNetworking.shareNetworking GET:url parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        weakself.displayEmpty = YES;
        
        [weakself.datas removeAllObjects];
        weakself.datas = [TJTopUpRecord mj_objectArrayWithKeyValuesArray:data[@"records"]];
    
        [weakself.tableView reloadData];
        [weakself footerViewIsHidden:weakself.datas];
        [weakself.tableView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
         weakself.displayEmpty = YES;
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData {
    _pageIndex++;
    
   
    NSDictionary *parameters = @{
        @"size": @(10),
        @"userId": TJUserInfo.readInfo.userId,
        @"page": @(_pageIndex)
     
    };
    
    NSString *url = @"";
       if (self.type == 0) {//提币
           url = @"member/balance/withdraw_recording";
       }else{//充币
            url = @"member/balance/recharge_recording";
       }
       
    
    weakify(self);
    [TJNetworking.shareNetworking GET:url parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        NSArray *arr = [TJTopUpRecord mj_objectArrayWithKeyValuesArray:data[@"records"]];
        
        [weakself.datas addObjectsFromArray:arr];
         [weakself footerViewIsHidden:arr];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView.mj_footer endRefreshing];
    }];
}


- (void)footerViewIsHidden:(NSArray *)arr {
    if (arr.count < 10) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        self.tableView.mj_footer.hidden = NO;
    }
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}


@end
